<template>
    <div v-if="isAuth(['ROOT', 'MEDICAL_DEPT:SELECT',])">
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
		    <el-form-item prop="name">
		        <el-input
		            v-model="dataForm.patientName"
		            placeholder="患者姓名"
		            size="medium"
		            class="input"
		            clearable="clearable"
		        />
		    </el-form-item>
			<el-form-item>
			    <el-button type="primary" @click="searchHandle()">查询</el-button>
			</el-form-item>
		</el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            :cell-style="{ padding: '3px 0' }"
            style="width: 100%;"
        >
            <el-table-column
                type="selection"
                :selectable="selectable"
                header-align="center"
                align="center"
                width="50"
            />
            <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                <template #default="scope">
                    <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
			<el-table-column header-align="center" align="center" label="挂号单号" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.id }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="患者编号" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.patientCardId }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="就诊时间" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.date }}</span>
			    </template>
			</el-table-column>
            <el-table-column
                prop="patientName"
                header-align="center"
                align="center"
                label="患者姓名"
                min-width="170"
                :show-overflow-tooltip="true"
            />
			<el-table-column header-align="center" align="center" label="诊室名称" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.subDeptname }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="医生姓名" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.doctorname }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="挂号单编号" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.outTradeNo }}</span>
			    </template>
			</el-table-column>
			<el-table-column prop="paymentStatus" header-align="center" align="center" label="挂号状态" min-width="120" />
            <el-table-column header-align="center" align="center" width="150" label="处方操作">
                <template #default="scope">
                    <el-button
                        type="text"
                        :disabled="!isAuth(['VIDEO_DIAGNOSE:SELECT'])"
                        @click="updateHandle(scope.row.id)"
                    >
                        添加
                    </el-button>
                    <el-button
                        type="text"
                        @click="getHandle(scope.row.id)"
                    >
                        查看
                    </el-button>
					<el-button
					    type="text"
					    :disabled="!isAuth(['ROOT'])"
					    @click="outHandle(scope.row.id)"
					>
					    出药
					</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            :total="totalCount"
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <add-or-update ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
		<select-getdate ref="selectGetdate" @refreshDataList="loadDataList"></select-getdate>
    </div>
</template>

<script>
import AddOrUpdate from './role-add-or-update.vue';
import SelectGetdate from './drug-select-getdate.vue';
export default {
    components: {
        AddOrUpdate,
		SelectGetdate
    },
    data: function() {
        return {
            dataForm: {
                patientName: '',
                paymentStatus: null
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalCount: 0,
            dataListLoading: false,
            dataListSelections: [],
            dataRule: {
                name: [{ required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,10}$', message: '部门名称格式错误' }]
            }
        };
    },
    methods: {
        loadDataList: function() {
            let that = this;
            that.dataListLoading = true;
            let data = {
                page: that.pageIndex,
                length: that.pageSize,
				patientName: that.dataForm.patientName == '' ? null : that.dataForm.patientName
            };
        
            that.$http('/registration/searchRegistrationByPages', 'POST', data, true, function(resp) {
                let result = resp.result;
				let temp = {
					'1': '失败',
					'2': '成功'
				}
                for (let one of result.list) {
                    one.paymentStatus = temp[one.paymentStatus+''];
                }
                that.dataList = result.list;
                that.totalCount = result.totalCount;
                that.dataListLoading = false;
            });
        },
		sizeChangeHandle: function(val) {
		    this.pageSize = val;
		    this.pageIndex = 1;
		    this.loadDataList();
		},
		currentChangeHandle: function(val) {
		    this.pageIndex = val;
		    this.loadDataList();
		},
		updateHandle: function(id) {
		    this.$nextTick(() => {
		        this.$refs.addOrUpdate.init(id);
		    });
		},
		getHandle: function(id) {
		    this.$nextTick(() => {
		        this.$refs.selectGetdate.init(id);
		    });
		},
		searchHandle: function() {
		    this.$refs['dataForm'].validate(valid => {
		        if (valid) {
		            this.$refs['dataForm'].clearValidate();
		            if (this.dataForm.patientName == '') {
		                this.dataForm.patientName = null;
		            }
		            if (this.pageIndex != 1) {
		                this.pageIndex = 1;
		            }
		            this.loadDataList();
		        } else {
		            return false;
		        }
		    });
		},
		selectionChangeHandle: function(val) {
		    this.dataListSelections = val;
		},
		outHandle: function(id) {
		    let that = this;
		    that.$confirm('确定出药？', '提示', {
		        confirmButtonText: '确定',
		        cancelButtonText: '取消',
		        type: 'warning'
		    }).then(() => {
				that.$http('/prescription/searchCount', 'POST', { id: id }, true, function(
				    resp
				) {
				    let result = resp.result;
					if(result==0){
						that.$http('/prescription/searchPrescriptionById', 'POST', { id: id }, true, function(resp) {
							let data={
								patientCardId: resp.patientCardId,
								subDeptId: resp.subDeptId,
								doctorId: resp.doctorId,
								id: resp.id
							}
							that.$http('/prescription/outDrug', 'POST', data, true, function(
							    resp
							) {
							    ElMessage({
							        message: '操作成功',
							        type: 'success',
							        duration: 1200,
							        onClose: () => {
							            that.loadDataList();
							        }
							    });
							});
						});
					}else{
						ElMessage({
						    message: '已出药，请勿重复操作',
						    type: 'fail'
						});
					}
				});
		    });
		},
    },
    created: function() {
        this.loadDataList();
    }
};
</script>

<style></style>
